package com.bbcare.switchData.controller;


import com.alibaba.fastjson.JSONObject;

import com.bbcare.comm.ResultModel;
import com.bbcare.comm.ToolUtils;
import com.bbcare.switchData.entity.model.Account;
import com.bbcare.switchData.entity.model.UserBaseInfo;
import com.bbcare.switchData.service.IQueryUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value="/switchData/queryUserInfo",produces = "application/json; charset=utf-8")
public class QueryUserInfo {



    @Autowired
    private IQueryUserInfoService queryUserInfoService;


    /**
     * 获得用户信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value="getUserInfoById",produces = "application/json; charset=utf-8")
    public ResultModel getUserInfoById(@RequestBody String requestBody){

        JSONObject params = JSONObject.parseObject(requestBody).getJSONObject("data");
        String userId = params.get("userId").toString();
        Map<String, Object> result = new HashMap<String, Object>();
        List<UserBaseInfo> list = queryUserInfoService.getUserInfoServiceById(userId);
        if( list!=null && list.size()==1){
            result.put("elements", list);
            return new ResultModel<Object>(result);
        }else{
            return new ResultModel<Object>("查询用户错误！请联系管理员"+list.size());
        }




    }

    /**
     * 获得用户信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value="getUserStatus",produces = "application/json; charset=utf-8")
    public ResultModel getUserStatus(@RequestBody String requestBody){

        JSONObject params = JSONObject.parseObject(requestBody).getJSONObject("data");
        String userId = params.get("userId").toString();
        Map<String, Object> result = new HashMap<String, Object>();
        String status = queryUserInfoService.getUserStatus(userId);
            result.put("status", status);
            return new ResultModel<Object>(result);
        }



    /**
     * 用户登录
     * @param phoneNo
     * @param password
     * @param httpSession
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping({"/login"})
    @ResponseBody
    public ResultModel login(String phoneNo, String password,String source , HttpSession httpSession, HttpServletRequest request)
            throws Exception
    {
        try{


            if(!source.equals("applet")||password == null || phoneNo ==null){

                return new ResultModel<Object>( "数据错误！");
            }

            Account account = queryUserInfoService.doLogin(phoneNo, password);
            if (account.userInfos==null){

                return new ResultModel<Object>("用户名或密码错误！");
            }

            return new ResultModel<Object>(account.userInfos);
        }catch (Exception e){
            return new ResultModel<Object>( "数据错误！");
        }
    }


    }



